package hc.bre

import static org.junit.jupiter.api.Assertions.*

import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest

import hc.bre.cmn.RuleDefinition
import hc.bre.project.ProjectDefinition
import hc.bre.rest.admin.BreAdminService
import hc.bre.rest.admin.ProjectEntry
import hc.bre.rest.manage.MainService
import hc.bre.runner.ClassLoaderRunner

@SpringBootTest
@groovy.transform.CompileStatic
class MainServiceTest {
	@Autowired
	MainService mainService;
	
	@Autowired
	BreAdminService adminService;

//	@Test TEMPXYZ
	void projectInit() {
		RuleDefinition r = new RuleDefinition();
		r.rpath = 'test/Rule'
		def p =  r.project;
		assert p != null;
		assert r.project == p;
	}

	@Test
	void project_load_print_json() {
		List<ProjectEntry> lst = adminService.config.projectList;
		if(!lst) return;

		ProjectDefinition prj = mainService.loadProject(lst[0].rpath)

//		assert prj == prj.project;
		assert prj.ruleList.size() ==	prj.rulePathMap.size()
		
		String stdout =
				hc.Util.captureStdOut {
					println prj;
					println hc.Util.jsonWrite(prj);
				};

		assert stdout.indexOf(prj.rpath)>=0;
	}

//	@Test
//	void loadAnyone() {
//		if(!mainService.repo.list)return;
//		RuleEntry r = mainService.repo.list[mainService.repo.list.size()-1];
//
//		RuleDefinition f1 = mainService.loadRuleDef(r.rpath);
//		assert r.rpath == f1.rpath;
//	}
	
//	@Test TEMPXYZ
	void classLoaderRunner()
	{
		 ClassLoaderRunner runner = new ClassLoaderRunner(ruledir:adminService.ruledir,rpath:'xsxdjc/UserInfo')
		 def ret = runner.run([userid:0])
		 assert ret['result']['data'] != null;
// 		 println ret;
	}
	
}
